import java.util.LinkedList;
import java.util.Queue;

public class MyStackUseQueue {
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    public MyStackUseQueue() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }
    int topVal = 0;
    public void push(int x) {
        if(!queue1.isEmpty()) {
            queue1.offer(x);
        } else if(!queue2.isEmpty()) {
            queue2.offer(x);
        } else {
            queue1.offer(x);
        }
    }

    public int pop() {
        if(empty()) {
            return -1;
        }
        if(!queue1.isEmpty()) {
                int size = queue1.size();
                for (int i = 0; i < size-1; i++) {
                    int val = queue1.poll();
                    queue2.offer(val);
                }
                return queue1.poll();
        }else {
                int size = queue2.size();
                for (int i = 0; i < size-1; i++) {
                    int val = queue2.poll();
                    queue1.offer(val);
                }
                return queue2.poll();
        }
    }

    public int top() {
        if(empty()) {
            return -1;
        }
        int tmp = 0;
        if(!queue1.isEmpty()) {
            int size = queue1.size();
            for (int i = 0; i < size; i++) {
                tmp = queue1.poll();
                queue2.offer(tmp);
            }
            return tmp;
        }else {
            int size = queue2.size();
            for (int i = 0; i < size; i++) {
                tmp = queue2.poll();
                queue1.offer(tmp);
            }
            return tmp;
        }
    }

    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }
}
